//获取datetime 格式为：2018-01-01 00:00:00
export function GetDatetime() {
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var hour = date.getHours();
	var minute = date.getMinutes();
	var second = date.getSeconds();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (day >= 0 && day <= 9) {
		day = "0" + day;
	}
	if (hour >= 0 && hour <= 9) {
		hour = "0" + hour;
	}
	if (minute >= 0 && minute <= 9) {
		minute = "0" + minute;
	}
	if (second >= 0 && second <= 9) {
		second = "0" + second;
	}
	return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' +
		second;
}

// 通过生日获取年龄 
export function GetAgeByBirthday(birthday) {
	const today = new Date();
	const birthDate = new Date(birthday);
	let age = today.getFullYear() - birthDate.getFullYear();
	const monthDifference = today.getMonth() - birthDate.getMonth();

	if (monthDifference < 0 || (monthDifference === 0 && today.getDate() < birthDate.getDate())) {
		age--;
	}
	return age;
}

// 通过身份证号获取年龄 
export function GetAgeByIdCard(idCard) {
	console.log('GetAgeByIdCard', idCard)
	if (!idCard) {
		return "无效的身份证号";
	}
	const birthYear = idCard.substring(6, 10);
	const birthMonth = idCard.substring(10, 12);
	const birthDay = idCard.substring(12, 14);

	if (!birthYear || !birthMonth || !birthDay) {
		return "无效的身份证号";
	}

	const today = new Date();
	const birthDate = new Date(birthYear + "/" + birthMonth + "/" + birthDay);

	if (isNaN(birthDate.getTime())) {
		return "无效的身份证号";
	}

	let age = today.getFullYear() - birthDate.getFullYear();
	const monthDifference = today.getMonth() - birthDate.getMonth();

	if (monthDifference < 0 || (monthDifference === 0 && today.getDate() < birthDate.getDate())) {
		age--;
	}

	return age;
}